﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.SharePoint;

using SO.SharePoint.Utilities;

namespace SO.SharePoint.ViewMigrator
{
    public partial class ExportForm : Form
    {
        public ExportForm()
        {
            InitializeComponent();
        }

        private void selectFolderButton_Click(object sender, EventArgs e)
        {
            if (selectFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                folderToSaveText.Text = selectFolderDialog.SelectedPath;
                ExportButton.Enabled = true;
            }
            else
            {
                if (folderToSaveText.Text == string.Empty)
                {
                    ExportButton.Enabled = false;
                }
            }
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ExportButton_Click(object sender, EventArgs e)
        {
            try
            {
                string folder = string.Concat(folderToSaveText.Text, @"\");
                string filename = string.Empty;
                int count = 0;
                ViewMigration vm = new ViewMigration();

                foreach (string view in viewList.CheckedItems)
                {
                    filename = string.Concat(folder, view, ".xml");                    
                    vm.ExportViewToFile((SPList)listDropDown.SelectedItem, view, filename);
                    count++;
                }

                MessageBox.Show(string.Format("{0} view(s) successfully exported", count),
                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Unknown error while exporting view to file", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void openWebButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (webUrlText.Text == string.Empty)
                {
                    webUrlText.Text = "/";
                }
                
                this.Cursor = Cursors.WaitCursor;
                SPSite site = new SPSite(siteUrlText.Text);
                if (site != null)
                {
                    SPWeb web = site.OpenWeb(webUrlText.Text);
                    if (web != null)
                    {
                        listDropDown.Items.Clear();
                        listDropDown.DisplayMember = "Title";
                        listDropDown.ValueMember = "ID";

                        foreach (SPList list in web.Lists)
                        {
                            if (list.Title != string.Empty)
                                listDropDown.Items.Add(list);
                        }

                        listDropDown.Enabled = true;
                        listDropDown.SelectedIndex = 0;
                    }
                    else
                    {
                        MessageBox.Show(string.Format("Can't open web '{0}'", webUrlText.Text),
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show(string.Format("Can't open site '{0}'", siteUrlText.Text),
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Unknown error connecting to site '{0}': {1}", siteUrlText.Text, ex.Message),
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = DefaultCursor;
            }
        }

        private void listDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                viewList.Items.Clear();
                viewList.Enabled = true;
                tabControl1.Enabled = true;

                foreach (SPView view in ((SPList)listDropDown.SelectedItem).Views)
                {
                    viewList.Items.Add(view.Title);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Unknown error while loading views from list '{0}': {1}", ((SPList)listDropDown.SelectedItem).Title, ex.Message),
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                importFileList.Items.Clear();
                foreach (string file in openFileDialog.FileNames)
                {
                    importFileList.Items.Add(file);
                }
            }
        }

        private void ImportButton_Click(object sender, EventArgs e)
        {
            try
            {
                ViewMigration vm = new ViewMigration();
                int count = 0;
                SPList list = (SPList)listDropDown.SelectedItem;
                string errorList = string.Empty;
                foreach (string file in importFileList.Items)
                {
                    try
                    {
                        vm.ImportViewFromFile(list, file);
                        count++;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(string.Format("Error while importing file '{0}': {1}", file, ex.Message), "Warning",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }

                MessageBox.Show(string.Format("{0} of {2} view(s) imported successfully to list '{1}'", count,
                    ((SPList)listDropDown.SelectedItem).Title, importFileList.Items.Count), "Information", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Unknown error while importing views to list: {0}", ex.Message), "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
